package project.taip.wawwe.db;

import java.util.ArrayList;
import java.util.Arrays;

import org.hibernate.Session;

import project.taip.wawwe.model.Ingredient;
import project.taip.wawwe.model.RateBean;
import project.taip.wawwe.model.Recipe;
import project.taip.wawwe.model.UserBean;
import project.taip.wawwe.util.HibernateHelper;

public class Services {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ArrayList<Recipe> recipes = browseRecipes("b");
		for (Recipe r : recipes) {
			System.out.println(r.getName() + " " + r.getDescription());
			for (RateBean rb : r.getRateBean()) {
				System.out.println("rated " + rb.getRate() +" stars by user "+
						" aged " + rb.getUserBean().getAge() +
						" from " + rb.getUserBean().getCountry() +
						" sex " + rb.getUserBean().getSex()
						);
			}
			/*
			UserBean ub = new UserBean();
			ub.setAge(12);
			ub.setCountry("Congo");
			ub.setSex("Male");
			rateRecipe(r, ub, 10);
			*/
		}

	}
	/**
	 * Submit a new recipe with optional submitter information
	 * @param recipe The new recipe
	 * @param submitter The bean containing submitter information
	 */
	public static void submitRecipe(Recipe recipe, UserBean submitter) {
		Session sess = null;
		try {
			sess = HibernateHelper.getInstance().getSession();
			sess.beginTransaction();
			recipe.setSubmitter(submitter);
			sess.save(recipe);
			sess.getTransaction().commit();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		finally {
			HibernateHelper.getInstance().closeSession();
		}
	}
	/**
	 * Rate a recipe
	 * @param recipe The rate recipe
	 * @param rate Rating value
	 * @param submitter The user that rated the recipe
	 */
	public static void rateRecipe(Recipe recipe, UserBean submitter, Integer rate ) {
		Session sess = null;
		try {
			sess = HibernateHelper.getInstance().getSession();
			sess.beginTransaction();
			RateBean rateBean = new RateBean();
			rateBean.setRecipe(recipe);
			rateBean.setUserBean(submitter);
			rateBean.setRate(rate);
			sess.save(rateBean);
			sess.getTransaction().commit();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		finally {
			HibernateHelper.getInstance().closeSession();
		}
	}
	/**
	 * Browse recipes by first letter(s) in title
	 * @param letter The first letter(s) in the recipe's title
	 * @return List of recipes that have titles starting with given letter(s)
	 */
	public static ArrayList<Recipe> browseRecipes(String letter) {
		Session sess = null;
		ArrayList<Recipe> recipes = null;
		try {
			sess = HibernateHelper.getInstance().getSession();
			recipes = (ArrayList<Recipe>)sess.createQuery("from Recipe where upper(name) like '"+ letter.toUpperCase() + "%' order by name asc").list();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		return recipes;
	}
	/**
	 * Search recipes by name, by ingredients, by rate or origin
	 * @param name Recipe name
	 * @param ingredients List of ingredients that must be found in recipe (only ingredient name)
	 * @param rate Minimum rate
	 * @param origin Recipe origin
	 * @return List of recipes that match criterias
	 */
	public static ArrayList<Recipe> searchRecipe(String name, ArrayList<Ingredient> ingredients, Integer rate, String origin ) {
		Session sess = null;
		ArrayList<Recipe> recipes = null;
		try {
			sess = HibernateHelper.getInstance().getSession();
			String queryString = "from Recipe r left join r.ingredients as i where";
			for (Ingredient ing : ingredients)
				queryString += "upper(i.name) like '" + ing.getName().toUpperCase() + "' and ";					
			if (rate != null && rate > 0)
				queryString += " r.rateBean.rate >= " + rate + " and ";
			if (origin != null && origin.length() > 0)
				queryString += " upper(r.origin) like '%" + origin.toUpperCase() + "%' "; 
			if (queryString.endsWith("and "))
				queryString = queryString.substring(0,queryString.indexOf("and "));
			recipes = (ArrayList<Recipe>)sess.createQuery(queryString).list();
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
		return recipes;
	}
}
